package com.offcn.question.controller;

import com.offcn.common.utils.PageUtils;
import com.offcn.common.utils.R;
import com.offcn.question.entity.QuestionEntity;
import com.offcn.question.entity.TypeEntity;
import com.offcn.question.service.QuestionService;
import com.offcn.question.service.TypeService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author yangguanlong
 * @email 8107626@qq.com
 * @date 2021-12-03 17:22:17
 */
@RestController
@RequestMapping("question/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = questionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        QuestionEntity question = questionService.getById(id);

        return R.ok().put("question", question);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody QuestionEntity question) {
        questionService.save(question);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody QuestionEntity question) {
        questionService.updateById(question);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        questionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/inporExcel")
    public R inporExcel(@RequestParam(name = "file") MultipartFile multipartFile) {
        Map<String, Object> map = questionService.importExcel(multipartFile);
        return R.ok().put("result", map.get("result")).put("msg", map.get("msg")).put("num", map.get("num"));
    }


    /**
     * 导出
     * @param tableName
     * @param response
     */
    @GetMapping("exportExcel")
    public void export(String tableName, HttpServletResponse response){
        System.out.println("导出excele");
        Workbook workbook = questionService.exportExcel();
        if (workbook != null) {
            String fileName = "uxue_" + new
                    SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xlsx";
            response.setHeader("Content-Disposition", "attachment;filename=" +
                    fileName);
            response.setContentType("application/octet-stream;charset=UTF-8");
            OutputStream outputStream;
            try {
                outputStream = response.getOutputStream();
                workbook.write(outputStream);
                outputStream.flush();
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                response.getWriter().print("error");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @Autowired
    private TypeService typeService;

    @RequestMapping("/countType")
    public R countType(){
        List<Map<String, Object>> list = questionService.countType();
        for (Map<String, Object> map : list) {
            //根据分类id，读取对应的分类数据
            Long typeId = (Long) map.get("TYPE");
            TypeEntity typeEntity = typeService.getById(typeId);
            //重新封装分类名称到map
            map.put("name",typeEntity.getType());

        }
        return R.ok().put("data" ,list);
    }

}
